﻿using System;
using System.Collections.Generic;
using System.Linq;
using ALC.Core.Enums;
using WPFCommon.ViewModels.Base;

namespace ALC.Core.ViewModels.Workpiece
{
    public class WorkpieceDictionaryViewModel : ViewModelBase
    {
        private readonly Dictionary<CavityType, WorkpieceViewModel> _workpieceViewModels  = new Dictionary<CavityType, WorkpieceViewModel>();
        public CavityType ActiveCavity { get; set; }

        public void SetActiveWorkpiece(CavityType cavityType, WorkpieceViewModel workpieceViewModel)
        {
            // Unhighlight all workpieces 
            foreach (var workpiece in _workpieceViewModels.Values)
            {
                if(workpiece == null) continue;
                workpiece.IsActive = false;
            }
            // Highlight new workpiece
            workpieceViewModel.IsActive = true;
            
            ActiveCavity = cavityType;
            _workpieceViewModels[cavityType] = workpieceViewModel;
            switch (cavityType)
            {
                case CavityType.Cavity1:
                    WorkpieceCavity1 = workpieceViewModel;
                    break;
                case CavityType.Cavity2:
                    WorkpieceCavity2 = workpieceViewModel;
                    break;
                default:
                    throw new ArgumentOutOfRangeException(nameof(cavityType), cavityType, null);
            }
        }

        public WorkpieceViewModel CurrentWorkpiece => _workpieceViewModels[ActiveCavity];

        public WorkpieceViewModel WorkpieceCavity1 { get; private set; }
        public WorkpieceViewModel WorkpieceCavity2 { get; private set; }
    }
}